package com.ruoyi.web.controller.shopping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.*;
import com.ruoyi.shopping.domain.MallOrder;
import com.ruoyi.shopping.domain.MallProductSpecsBack;
import com.ruoyi.shopping.service.IMallOrderService;
import com.ruoyi.shopping.service.IMallProductSpecsBackService;
import com.ruoyi.shopping.vo.MallOrderListVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.shopping.domain.MallOrderMesh;
import com.ruoyi.shopping.service.IMallOrderMeshService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.naming.Name;

/**
 * 商城订单合并下单Controller
 *
 * @author ruoyi
 * @date 2022-06-09
 */
@RestController
@RequestMapping("/shopping/mallOrderMesh")
public class MallOrderMeshController extends BaseController
{
    @Autowired
    private IMallOrderMeshService mallOrderMeshService;
    @Autowired
    private IMallOrderService mallOrderService;
    @Autowired
    private IMallProductSpecsBackService backService;




    /**
     * 根据商城订单传回来的合并订单查询
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallOrderMesh:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallOrderMesh mallOrderMesh)
    {
        startPage();
//        if(StringUtils.isNotEmpty(mallOrderMesh.getOrderState())){
//            switch (mallOrderMesh.getOrderState()) {
//                case "await_evaluate":
//                    mallOrderMesh.setCommentState(DicConstants.SYS_N.getCode());
//                    mallOrderMesh.setOrderState(DicConstants.ORDER_STATE_Received.getCode());
//                    break;
//                case "accomplish":
//                    mallOrderMesh.setOrderState(DicConstants.ORDER_STATE_FINISHED.getCode());
//                    break;
//            }
//        }

        if(StringUtils.isNotEmpty(mallOrderMesh.getCombineOrder())){
            MallOrder mallOrder = mallOrderService.selectByOrderId(mallOrderMesh.getCombineOrder());
            mallOrderMesh.setId(mallOrder.getMergeOrderId());
        }
        List<MallOrderMesh> list = mallOrderMeshService.selectMallOrderMeshList(mallOrderMesh);//查询所有合并订单信息
        List<MallOrderListVo> listResult=new ArrayList<>();
        for (MallOrderMesh orderMesh : list) {
            Long MallOrderMeshId=orderMesh.getId();//把查询到的合并订单里的id给商城订单
            List<MallOrder> mallOrderList= mallOrderService.selectMallOrderByMeshId(MallOrderMeshId);//查询商城订单信息
            List<MallOrderListVo> mallOrderListVos= CopyUtils.copyList(mallOrderList,MallOrderListVo.class);
            if(mallOrderListVos.size()>0){
                mallOrderListVos.get(0).setNums(mallOrderListVos.size());
            }
            for (MallOrderListVo vo : mallOrderListVos) {
                vo.setMallOrderMeshId(MallOrderMeshId);
                vo.setMallOrderMeshOrderId(orderMesh.getOrderId());
                vo.setMeshState(orderMesh.getOrderState());
                MallProductSpecsBack back = backService.selectMallProductSpecsBackById(vo.getSpecsBackId());
                if(back!=null){
                    vo.setProductInfo(new StringBuilder(vo.getProductName()).append(" | ").append(back.getSpecsName()+" ").append(back.getCurrentPrice()).append("×").append(vo.getNum()).toString());
                }
                vo.setDate(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",orderMesh.getCreateTime()));
                vo.setPayPrice(orderMesh.getPayPrice().setScale(2, BigDecimal.ROUND_DOWN));
                vo.setOrderState(vo.getOrderState());
                if(DicConstants.SYS_Y.getCode().equals(vo.getAfterSalesState())){
                    vo.setOrderState(vo.getRefundStatus());
                }else if(DicConstants.ORDER_STATE_Wait_Pay.getCode().equals(orderMesh.getOrderState())){
                    vo.setOrderState(orderMesh.getOrderState());
                }
                if(DicConstants.ORDER_STATE_FINISHED.getCode().equals(orderMesh.getOrderState()) && DicConstants.SYS_N.getCode().equals(orderMesh.getPayState())){
                    vo.setOrderState("cancel");
                }
                vo.setId(orderMesh.getId());
            }
            listResult.addAll(mallOrderListVos);
        }
        TableDataInfo tableDataInfo= getDataTable (list);
        tableDataInfo.setRows(listResult);
        return tableDataInfo;

    }




    /**
     * 导出商城订单合并下单列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallOrderMesh:export')")
    @Log(title = "商城订单合并下单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallOrderMesh mallOrderMesh)
    {
        List<MallOrderMesh> list = mallOrderMeshService.selectMallOrderMeshList(mallOrderMesh);
        ExcelUtil<MallOrderMesh> util = new ExcelUtil<MallOrderMesh>(MallOrderMesh.class);
        return util.exportExcel(list, "商城订单合并下单数据");
    }

    /**
     * 获取商城订单合并下单详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallOrderMesh:query')")
    @GetMapping(value = "/{ids}")
    public AjaxResult getInfo(@PathVariable("ids") Long id)
    {
        MallOrderMesh mallOrderMesh = mallOrderMeshService.selectMallOrderMeshById(id);
        mallOrderMesh.setPayState(DictUtils.getDictLabel("huifu_pay_state",mallOrderMesh.getPayState()));
        mallOrderMesh.setPayType(DictUtils.getDictLabel("sys_pay_type",mallOrderMesh.getPayType()));

        return AjaxResult.success(mallOrderMesh);
    }

    /**
     * 新增商城订单合并下单
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallOrderMesh:add')")
    @Log(title = "商城订单合并下单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallOrderMesh mallOrderMesh)
    {
        return toAjax(mallOrderMeshService.insertMallOrderMesh(mallOrderMesh));
    }

    /**
     * 修改商城订单合并下单
     */
    @GetMapping("/updateOrderMesh")
    public AjaxResult updateOrderMesh( Long id ,String remark)
    {
        MallOrderMesh mallOrderMesh = new MallOrderMesh();
        mallOrderMesh.setId(id);
        mallOrderMesh.setPlatformRemark(remark);
        return toAjax(mallOrderMeshService.updateMallOrderMesh(mallOrderMesh));
    }


    /**
     * 修改商城订单合并下单
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallOrderMesh:edit')")
    @Log(title = "商城订单合并下单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallOrderMesh mallOrderMesh)
    {
        mallOrderMesh.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(mallOrderMeshService.updateMallOrderMesh(mallOrderMesh));
    }

    /**
     * 删除商城订单合并下单
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallOrderMesh:remove')")
    @Log(title = "商城订单合并下单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallOrderMeshService.logicDeleteMallOrderMesh(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
